
#import "ActionShareViewController.h"
#import "ActivityDetailsViewController.h"
#import "netWorkHandle.h"
#import "Activity.h"
#import <sqlite3.h>

@interface ActionShareViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITableView *tableV;

@end

@implementation ActionShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    /********************************* 活动收藏界面 *************************************/
    
    self.navigationItem.title = @"活动收藏";
    [self getActArr];
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [self.tableV release];
    
}

//// 下拉刷新
//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    //    if (self.m_data.count - 1 == indexPath.row) {
//    //
//    //        [self.tableV reloadData];
//    //    }
//    
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 62;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.arr count];
}

// 获取收藏数组
- (void)getActArr {
    
    static sqlite3 *DB = nil;
    
    // 打开数据库
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES) lastObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"dataBase.db"];
    sqlite3_open(filePath.UTF8String, &DB);
    
    // 查找已登录用户收藏的活动
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM activityTable WHERE  userName LIKE '%%%@%%'", self.name];
    sqlite3_stmt *stmt = nil;
    sqlite3_prepare(DB, sqlStr.UTF8String, -1, &stmt, NULL);
    self.arr = [NSMutableArray array];
    while (SQLITE_ROW == sqlite3_step(stmt)) {
        
        Activity *act = [[Activity alloc] init];
        const unsigned char *act1 = sqlite3_column_text(stmt, 2);
        const unsigned char *act2 = sqlite3_column_text(stmt, 3);
        const unsigned char *act3 = sqlite3_column_text(stmt, 4);
        const unsigned char *act4 = sqlite3_column_text(stmt, 5);
        const unsigned char *act5 = sqlite3_column_text(stmt, 6);
        const unsigned char *act8 = sqlite3_column_text(stmt, 7);
        const unsigned char *act9 = sqlite3_column_text(stmt, 8);
        const unsigned char *act10 = sqlite3_column_text(stmt, 9);
        
        act.title = [NSString stringWithUTF8String:(const char *)act1];
        act.begin_time = [NSString stringWithUTF8String:(const char *)act2];
        act.end_time = [NSString stringWithUTF8String:(const char *)act3];
        act.address = [NSString stringWithUTF8String:(const char *)act4];
        act.category_name = [NSString stringWithUTF8String:(const char *)act5];
        act.image = [NSString stringWithUTF8String:(const char *)act8];
        act.name = [NSString stringWithUTF8String:(const char *)act9];
        act.content = [NSString stringWithUTF8String:(const char *)act10];
        
        [self.arr addObject:act];
    }
    sqlite3_close(DB);
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    [self.tableV setEditing:editing animated:animated];
    
}

// 删除活动收藏
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static sqlite3 *DB = nil;
    Activity *act = [self.arr objectAtIndex:indexPath.row];
    
    // 打开数据库
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES) lastObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"dataBase.db"];
    sqlite3_open(filePath.UTF8String, &DB);
    
    // 找到符合的用户名和收藏名
    NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM activityTable WHERE userName = '%@' and title = '%@'", self.name, act.title];
    sqlite3_exec(DB, sqlStr.UTF8String, NULL, NULL, NULL);
    sqlite3_close(DB);
    
    // 在数据库删除数据后删除数组中的数据!!
    [self.arr removeObjectAtIndex:indexPath.row];
    [self.tableV reloadData];
}

// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellStr = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    
    if (nil == cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    
    Activity *act = [self.arr objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = act.title;
    
    return cell;
}

// 切换到详细页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ActivityDetailsViewController *actVC = [[ActivityDetailsViewController alloc] init];
    actVC.act = [self.arr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:actVC animated:YES];
    [actVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end